import {
  IsOptional,
  IsString,
  IsNumber,
  IsObject,
  IsArray,
} from 'class-validator';

/**
 * 统一的API响应格式
 */
export class ApiResponse<T> {
  /**
   * HTTP状态码
   */
  @IsNumber()
  statusCode: number;

  /**
   * 响应消息
   */
  @IsString()
  message: string;

  /**
   * 响应数据，可以是单个对象或数组
   */
  @IsOptional()
  @IsObject()
  @IsArray()
  data?: T | T[];

  /**
   * 错误信息，仅在请求失败时提供
   */
  @IsOptional()
  @IsString()
  error?: string;

  /**
   * 详细错误信息，可以是字符串或数组，用于展示具体的参数验证错误
   */
  @IsOptional()
  errors?: string | string[];

  /**
   * 时间戳
   */
  timestamp: string;

  /**
   * API路径
   */
  path: string;

  constructor(
    statusCode: number,
    message: string,
    data?: T | T[],
    error?: string,
    path?: string,
    errors?: string | string[],
  ) {
    this.statusCode = statusCode;
    this.message = message;
    this.data = data;
    this.error = error;
    this.errors = errors;
    this.timestamp = new Date().toISOString();
    this.path = path || '';
  }
}
